import requests
from Read.read_yaml import read_config
class ApiBase:

    def __init__(self,env="test"):
        config_path=r"D:\桌面\Project\PythonProject2\ApiBase\config.yaml"
        config=read_config(config_path,env=env)
        self.url=config['base_url']
        self.session=requests.session()


    def request(self,method,path,**kwargs):
        url=f'{self.url}{path}'
        print('当前使用的url:',url,'\n')
        response=self.session.request(method,url,**kwargs)
        return response

    def post(self,path,**kwargs):
        return self.request('POST',path,**kwargs)

    def get(self,path,**kwargs):
        return self.request('GET',path,**kwargs)

    def delete(self,path,**kwargs):
        return self.request('DELETE',path,**kwargs)

    def put(self,path,**kwargs):
        return self.request('PUT',path,**kwargs)

# import requests
# from Read.read_yaml import read_config
#
# class ApiBase:
#     def __init__(self, env="test"):
#         # 读取环境配置
#         config = read_config(r"D:\桌面\Project\PythonProject2\ApiBase\config.yaml", env=env)
#         self.base_url = config["base_url"]
#         self.session = requests.session()
#         # 自动执行登录
#         self.login()
#
#
#
#     def request(self, method, path, **kwargs):
#         """通用请求方法，封装URL和会话"""
#         url = f"{self.base_url}{path}"
#         response = self.session.request(method, url, **kwargs)
#         return response
#
#     def login(self):
#         """登录逻辑，获取并维护Cookie"""
#         login_path = "roncoo-pay-web-boss/login"  # 登录接口路径
#         login_data = {
#             "loginName": "admin",   # 替换为实际账号
#             "roncooPwd": "123456"    # 替换为实际密码
#         }
#         # 发送登录请求
#         login_response = self.session.post(
#             url=f"{self.base_url}{login_path}",
#             data=login_data
#         )
#         # 验证登录是否成功
#         assert login_response.status_code == 200, "登录接口调用失败"
#
#     def post(self, path, **kwargs):
#         return self.request("POST", path, **kwargs)
#
#     def get(self, path, **kwargs):
#         return self.request("GET", path, **kwargs)
#
#     def put(self, path, **kwargs):
#         return self.request("PUT", path, **kwargs)
#
#     def delete(self, path, **kwargs):
#         return self.request("DELETE", path, **kwargs)

